package com.mdp.workflow.biz.ru.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.Result;
import com.mdp.core.err.BizException;
import com.mdp.core.query.QueryTools;
import com.mdp.core.utils.ObjectTools;
import com.mdp.core.utils.RequestUtils;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.swagger.ApiEntityParams;
import com.mdp.workflow.api.FlowExecutionService;
import com.mdp.workflow.biz.ru.entity.Execution;
import com.mdp.workflow.biz.ru.vo.ProcStartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/*/ru/execution")
@Api(tags={"act_ru_execution-操作接口"})
public class ExecutionController {
	
	static Logger logger =LoggerFactory.getLogger(ExecutionController.class);
	
	@Autowired
	private FlowExecutionService executionService;

	@Value(value = "${mdp.platform-branch-id:platform-branch-001}")
	String platformBranchId;

	@ApiOperation( value = "act_ru_execution-查询列表",notes=" ")
	@ApiEntityParams(Execution.class)
	@ApiResponses({
		@ApiResponse(code = 200,response=Execution.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listExecution(@ApiIgnore @RequestParam Map<String,Object> params){
		try {
			User user=LoginUtils.getCurrentUserInfo();
			params.remove("tenantId");
			RequestUtils.transformArray(params, "categoryIds");
			RequestUtils.transformArray(params, "tagIds");
			RequestUtils.transformArray(params, "procInstIds");
			QueryWrapper<Execution> qw = QueryTools.initQueryWrapper(Execution.class , params,"res.*","pa.*","p.*").in("res.tenant_id_",platformBranchId,user.getBranchId());
			IPage page=QueryTools.initPage(params);
			List<Map<String,Object>> datas = executionService.selectListMapByWhere(page,qw,params);
			return Result.ok("query-ok","查询成功").setData(datas).setTotal(page.getTotal());
		}catch (BizException e) {
			return Result.error(e);
		}catch (Exception e) {
			return Result.error(e);
		}
	}

	/**
	 * 获取启动流程所需参数
	 * @return
	 */
	@RequestMapping(value="/startPre")
	public Result getStartPreParams(String procDefId){
		return executionService.startPre(procDefId);
	}


	@RequestMapping(value="/start")
	public Result start(@RequestBody ProcStartVo procStartVo) {
		Result result=executionService.start(procStartVo);
		Result.assertIsFalse(result.getTips());
		return Result.ok().setData(procStartVo);
	}

	@RequestMapping(value="/suspend")
	public Result suspend(@RequestBody Execution excution) {

		try{
			if(ObjectTools.isEmpty(excution.getProcInstId())){
				return Result.error("procInstId-required","流程实例编号不能为空");
			}
			return executionService.suspendProcessInstance(excution.getProcInstId());
		}catch (BizException e) {
			logger.error("执行异常",e);
			return Result.error(e);
		}catch (Exception e) {
			logger.error("执行异常",e);
			return Result.error(e.getMessage());
		}
	}
	@RequestMapping(value="/activite")
	public Result activate(@RequestBody Execution excution) {

		try{
			if(ObjectTools.isEmpty(excution.getProcInstId())){
				return Result.error("procInstId-required","流程实例编号不能为空");
			}
			return executionService.activateProcessInstance(excution.getProcInstId());
		}catch (BizException e) {
			logger.error("执行异常",e);
			return Result.error(e);
		}catch (Exception e) {
			logger.error("执行异常",e);
			return Result.error(e.getMessage());
		}
	}
	@RequestMapping(value="/del",method = RequestMethod.POST)
	public Result delete(@RequestBody Execution excution) {

		try{
			if(ObjectTools.isEmpty(excution.getProcInstId())){
				return Result.error("procInstId-required","流程实例编号不能为空");
			}
			return executionService.delExecution(excution.getProcInstId(),"删除",new HashMap<>());
		}catch (BizException e) {
			logger.error("执行异常",e);
			return Result.error(e);
		}catch (Exception e) {
			logger.error("执行异常",e);
			return Result.error(e.getMessage());
		}
	}

}
